#include <iostream>
using namespace std;

class Person
{
public:
    Person()
    {
        cout<<"Person()"<<endl;
    }
    virtual ~Person()
    {
        cout<<"~Person()"<<endl;
    }

    virtual void run()
    {
        cout<<"Person::run()"<<endl;
    }
};

class Student:public Person
{
public:
    Student()
    {
        cout<<"Student()"<<endl;
    }
    ~Student()
    {
        cout<<"~Student()"<<endl;
    }

    void run()
    {
        cout<<"Student::run()"<<endl;
    }

    void study()
    {
        cout<<"Student::study()"<<endl;
    }

public:
    int m_score;
};

int main()
{
    #if 1
    Person *p1 = new Student();
    p1->run();
    delete p1;
    #else
    Student *stu1 = new Person();
    stu1->run();
    delete stu1;
    #endif

    Person * p1 = new Person();//
    Person *p2 = new Student(); //父类指针指向子类对象

    #if 0
    //错误示范
    Student *stu1 = p1;
    Student *stu1 = p2;
    #endif
    //运行时安全检测
    Student *stu1 = dynamic_cast<Student*>(p1);
    cout<<"stu1:"<<stu1<<endl;
    stu1->run();
    //错误使用内存，踩内存
    stu1->m_score = 323;

    /*内存泄漏
    1、野指针
    2、malloc出的空间没有释放
    3、踩内存:（其中子类指针指向父类对象没有使用dynamic_cast或许会踩内存）
    */
    Student *stu2 = dynamic_cast<Student*>(p2);
    cout<<"stu2:"<<stu2<<endl;

    
}